use crate::topic::Topic;

pub struct SimpleConsumer<'a>{
    name: String,
    topic: &'a mut Topic
}

impl<'a> SimpleConsumer<'a> {
    pub fn new(name: &str, topic :&'a mut Topic) -> Self {
        Self {
            name: String::from(name),
            topic: topic
        }
    }
    pub fn get_name(&self) -> &str {
        self.name.as_str()
    }
    pub fn get_topic(&self) -> & Topic {
        self.topic
    }
}


pub use crate::consumer::{Consumer, CommonError};
impl<'a> Consumer for SimpleConsumer<'a> {
    fn consume(&mut self, buf: &mut [u8]) -> Result<usize, CommonError> {
        self.topic.remove(buf)
    }
}

#[cfg(test)]
mod test  {
    use super::*;
    #[test]
    fn test_consumer(){
        let mut topic = Topic::new("topic_name", ".");
        let mut sc = SimpleConsumer::new("consumer", &mut topic);

        let mut buf = [23; 100];
        let read_bytes = sc.consume(&mut buf).unwrap();
        assert_eq!(0, read_bytes);
    }

    #[test]
    fn test_use_trait_consumer(){
        //use trait only
        fn use_consumer<T: Consumer>(c: &mut T) {
            let mut buf = [23; 1];
            let read_bytes = c.consume(&mut buf).unwrap();
            assert_eq!(0, read_bytes);
        }

        let mut topic = Topic::new("topic_name", ".");
        assert_eq!(0, topic.len());
        let mut sc = SimpleConsumer::new("consumer", &mut topic);
        use_consumer(&mut sc);
    }
}
